package org.shopen.puremvc.view
{
	import mx.collections.ArrayCollection;
	import mx.events.ItemClickEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import org.shopen.puremvc.ApplicationConstants;
	import org.shopen.puremvc.model.TestingProxy;
	import org.shopen.puremvc.model.domain.testing.Testing;
	import org.shopen.puremvc.view.components.ItemSelector;

	public class TestingSelectorMediator extends Mediator
	{
		public static const NAME : String = "TestSelectorMediator";
		
		public function TestingSelectorMediator(viewComponent:ItemSelector=null)
		{
			super(TestingSelectorMediator.NAME, viewComponent);
		}
		
		override public function getMediatorName():String
		{
			return TestingSelectorMediator.NAME;
		}
		
		[Bindable]
		private var _testings : ArrayCollection = new ArrayCollection();
		[Bindable]
		protected function get testings() : ArrayCollection
		{
			return _testings;
		} 
		protected function set testings(value : ArrayCollection) : void
		{
			_testings = value;
		}
		
		protected function get selector() : ItemSelector
		{
			return viewComponent as ItemSelector;
		}
		
		protected function get selectedIndex() : Number
		{
			var result : Number = selector.selectedIndex;
			return result;
		}
		protected function set selectedIndex(value : Number) : void
		{
			if (value >= 0)			
				selector.selectedIndex = value;
			else
				selector.selectedIndex = -1; //deselect
		}
		
		protected function get currentItem() : Testing
		{
			var result : Testing = null;
			if (selectedIndex != -1)
			{
				result = testings.getItemAt(selectedIndex) as Testing;
			}
			return result;
		}
		
		override public function listNotificationInterests():Array
		{
			return [TestingProxy.TESTINGS_INITIALIZED,
					ApplicationConstants.SELECT_STAGE,
					ApplicationConstants.SELECT_TESTING,
					ApplicationConstants.START_SCORING,
					ApplicationConstants.START_TESTING];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case TestingProxy.TESTINGS_INITIALIZED:
					initTestings(notification.getBody() as ArrayCollection);
					break;
				case ApplicationConstants.SELECT_STAGE:
					selectedIndex = -1;
					break;
				case ApplicationConstants.START_TESTING:
				case ApplicationConstants.SELECT_TESTING:
					selector.enabled = false;
					break;
				case ApplicationConstants.START_SCORING:
					selector.enabled = true;
					break;
			}
		}
		
		private function initTestings(testingsArr : ArrayCollection) : void
		{
			testings = testingsArr;
			selector.dataProvider = testings;
			selector.labelField = "title";
			selector.toolTipField = "title";
			selector.addEventListener(ItemClickEvent.ITEM_CLICK, itemClickListener);
		}		
		
		private function itemClickListener(event : ItemClickEvent) : void
		{
			if (selectedIndex != -1)
			{
				sendNotification(ApplicationConstants.SELECT_TESTING, currentItem);
			}
		}
	}
}